﻿/* Csv操作类
 */

/*待测试*/
namespace LSLib.Data
{
    using System;
    using System.Data;
    using System.IO;
    using System.Text;

    public static class CsvHelper
    {
        /// <summary>将DataTable导出为Csv
        /// </summary>
        /// <param name="dataTable">DataTable</param>
        /// <param name="fileName">Csv保存路径</param>
        /// <param name="tableHeader">表头</param>
        /// <param name="columName">字段标题,逗号分隔</param>
        public static bool Data2Csv(DataTable dataTable, string fileName, string tableHeader, string columName)
        {
            if (dataTable == null) throw new ArgumentNullException("dataTable");
            if (string.IsNullOrEmpty(fileName)) throw new ArgumentNullException("fileName");

            int bufSize = 1024;
            StringBuilder strBufferLine = new StringBuilder(bufSize);
            try
            {
                using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.UTF8))
                {
                    sw.WriteLine((tableHeader != null) ? tableHeader : "Default TableHeader");
                    sw.WriteLine((columName != null) ? columName : "Default ColumName");

                    for (int i = 0; i < dataTable.Rows.Count; i++)
                    {
                        strBufferLine = new StringBuilder(bufSize);
                        for (int j = 0; j < dataTable.Columns.Count; j++)
                        {
                            if (j > 0) strBufferLine.Append(",");
                            strBufferLine.Append(dataTable.Rows[i][j].ToString());
                        }
                        sw.WriteLine(strBufferLine.ToString());
                    }
                }
                return true;
            }
            catch (Exception) { throw; }
        }

        /// <summary>将Csv读入DataTable
        /// </summary>
        /// <param name="fileName">csv文件路径</param>
        /// <param name="titleRows">标题所占的行数为n,第n+1行是记录开始</param>
        public static DataTable Csv2Data(string fileName, int titleRows)
        {
            if (File.Exists(fileName) == false) throw new FileNotFoundException("未找到指定的CSV文件!", fileName);
            if (titleRows < 0) throw new ArgumentOutOfRangeException("titleRows", titleRows, "titleRows 不能是负数!");

            DataTable result = new DataTable();
            using (StreamReader sr = new StreamReader(fileName, Encoding.UTF8, false))
            {
                int cnt = 0;
                string[] split;
                DataRow dataRow;

                for (cnt = 0; cnt < titleRows; cnt++)
                {
                    if (sr.Peek() == -1) break;
                    sr.ReadLine();//去除标题所占的行
                }

                while (sr.Peek() > 0)
                {
                    split = sr.ReadLine().Split(',');

                    dataRow = result.NewRow();
                    for (cnt = 0; cnt < split.Length; cnt++)
                    {
                        dataRow[cnt] = split[cnt];
                    }
                    result.Rows.Add(dataRow);
                }
            }
            return result;
        }
    }
}